<template>
  <div class="login-container">
    <a-card class="login-card" :bordered="false">
      <template #title>
        <div class="card-title">
          <h2>抽奖系统管理后台</h2>
        </div>
      </template>
      <a-form
        :model="form"
        :rules="rules"
        ref="formRef"
        @finish="handleLogin"
        layout="vertical"
      >
        <a-form-item name="username" label="用户名">
          <a-input
            v-model:value="form.username"
            placeholder="请输入用户名"
            size="large"
          >
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item name="password" label="密码">
          <a-input-password
            v-model:value="form.password"
            placeholder="请输入密码"
            size="large"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            :loading="loading"
            size="large"
            block
          >
            {{ loading ? '登录中...' : '登录' }}
          </a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import type { FormInstance } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import request from '../utils/request'

const router = useRouter()
const formRef = ref<FormInstance>()
const loading = ref(false)

const form = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名' }],
  password: [{ required: true, message: '请输入密码' }]
}

const handleLogin = async () => {
  loading.value = true
  try {
    const res = await request.post('/api/lottery/admin/login', form)
    console.log(res)
    if (res.code === 200) {
      localStorage.setItem('token', res.data)
      message.success(res.message || '登录成功')
      router.push('/admin/users')
    } else {
      message.error(res.message || '登录失败')
    }
  } catch (error: any) {
    message.error('登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1890ff 0%, #722ed1 100%);
  margin: 0;
  padding: 0;
  position: fixed;
  top: 0;
  left: 0;
}

.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  margin: 0;
  padding: 0;
}

.card-title {
  text-align: center;
  margin-bottom: 24px;
}

.card-title h2 {
  margin: 0;
  color: #1890ff;
  font-size: 24px;
}

:deep(.ant-card-head-title) {
  text-align: center;
  padding: 12px 0;
}

:deep(.ant-form-item-label) {
  padding-bottom: 8px;
}

:deep(.ant-input-affix-wrapper) {
  padding: 8px 11px;
}

:deep(.ant-btn) {
  height: 40px;
  font-size: 16px;
}
</style> 